#include "CFileFinder.h"
#include <stack>
#include <fstream>

void DeleteSubItem(IN std::string strDirPath)
{
    std::list<std::string> FileList;
    std::list<std::string> DirList;
    std::stack<std::string> DirStack;
    std::string strFileName;
    std::string strRootDirName;
    std::string strSubDirName;
    WIN32_FIND_DATAA Win32FindData = { 0 };
    HANDLE hFile;
    size_t stSize;

    CFileFinder::TraverseDirectory(strDirPath, &FileList, nullptr, TRUE);

    for (auto it = FileList.begin(); it != FileList.end(); ++it)
        DeleteFileA(it->c_str());

    DirList.push_back(strDirPath);
    while (TRUE)
    {
        if (!DirList.empty())
        {
            strRootDirName = DirList.front();
            DirList.pop_front();
        }
        else
        {
            break;
        }

        if (strRootDirName.c_str()[strRootDirName.length() - 1] == '\\')
            strRootDirName.pop_back();

        strSubDirName = strRootDirName;
        strSubDirName += "\\*.*";

        hFile = FindFirstFileA(strSubDirName.c_str(), &Win32FindData);
        if (hFile == INVALID_HANDLE_VALUE) continue;

        do
        {
            strFileName = Win32FindData.cFileName;
            if (strFileName == "." || strFileName == "..") continue;

            if (Win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                strFileName = strRootDirName + "\\" + strFileName;
                DirStack.push(strFileName);
                DirList.push_back(strFileName);
            }

        } while (FindNextFileA(hFile, &Win32FindData));

        FindClose(hFile);
    }

    stSize = DirStack.size();
    for (size_t i = 0; i < stSize; ++i)
    {
        std::string strDirName = DirStack.top();
        DirStack.pop();
        RemoveDirectoryA(strDirName.c_str());
    }
}

BOOL GetDirNameFromCfg(std::string strCurrentPath, std::list<std::string>& lst)
{
    std::string strCfgFile;
    std::string strKey;
    std::ifstream is;
    char strBuff[1024] = { 0 };
    size_t stPos;

    strCfgFile = strCurrentPath + "\\";
    strCfgFile += "Dirs.cfg";

    is.open(strCfgFile.c_str(), std::ios::in | std::ios::binary);
    if (!is.is_open()) return FALSE;

    while (is.getline(strBuff, 1024))
    {
        strKey = strBuff;
        stPos = strKey.find_last_of("\r");
        if (stPos != std::string::npos)
            strKey = strKey.substr(0, stPos);
        lst.push_back(strKey);
    }

    return TRUE;
}

int main()
{
    CHAR strBuff[MAX_PATH] = { 0 };
    std::list<std::string> DirNameList;
    std::string strCurrentPath;
    std::list<std::string> DirList;
    std::string strDirName;
    size_t stPos;
    size_t stLen;

    GetCurrentDirectoryA(MAX_PATH, strBuff);
    strCurrentPath = strBuff;

    GetDirNameFromCfg(strCurrentPath, DirNameList);
    if (!DirNameList.size()) return 0;

    CFileFinder::TraverseDirectory(strCurrentPath, nullptr, &DirList, TRUE);

    for (auto it = DirList.begin(); it != DirList.end(); ++it)
    {
        strDirName = it->c_str();
        stPos = strDirName.find_last_of('\\');
        stLen = strDirName.length();
        strDirName = strDirName.substr(stPos + 1, stLen - stPos);

        for (auto it2 = DirNameList.begin(); it2 != DirNameList.end(); ++it2)
        {
            if (strDirName == *it2)
            {
                DeleteSubItem(*it);
                RemoveDirectoryA(it->c_str());
                break;
            }
        }
    }

    system("pause");

    return 0;
}